package com.foreknow.elm.dao.impl;

import com.foreknow.elm.dao.CartDao;
import com.foreknow.elm.po.Business;
import com.foreknow.elm.po.Cart;
import com.foreknow.elm.po.Food;
import com.foreknow.elm.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class CartDaoImpl implements CartDao {
    private Connection con = null;
    private PreparedStatement pst = null;
    private ResultSet rs = null;
    @Override
    public List<Cart> listCart(Cart userId) throws Exception {
        List<Cart> list = new ArrayList<>();
        String sql = "SELECT c.*,b.businessName businessName,b.businessAddress businessAddress,b.businessExplain businessExplain," +
                "b.businessImg businessImg,b.orderTypeId orderTypeId,b.starPrice starPrice,b.deliveryPrice deliveryPrice,b.remarks remarks," +
                "f.foodName foodName,f.foodExplain foodExplain,f.foodImg foodImg,f.foodPrice foodPrice " +
                "from (cart c left join business b on c.businessId=b.businessId) left join food f on c.foodId = f.foodId" +
                "  where c.userId=?";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setString(1, String.valueOf(userId));
            rs = pst.executeQuery();
            while (rs.next()){
                Business business = new Business();
                business.setBusinessId(rs.getInt("businessId"));
                business.setBusinessName(rs.getString("businessName"));
                business.setBusinessAddress(rs.getString("businessAddress"));
                business.setBusinessExplain(rs.getString("businessExplain"));
                business.setBusinessImg(rs.getString("businessImg"));
                business.setOrderTypeId(rs.getInt("orderTypeId"));
                business.setStarPrice(rs.getDouble("starPrice"));
                business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
                business.setRemarks(rs.getString("remarks"));

                Food food = new Food();
                food.setFoodId(rs.getInt("foodId"));
                food.setFoodName(rs.getString("foodName"));
                food.setFoodExplain(rs.getString("foodExplain"));
                food.setFoodImg(rs.getString("foodImg"));
                food.setFoodPrice(rs.getDouble("foodPrice"));
                food.setBusinessId(rs.getInt("businessId"));
                food.setRemarks(rs.getString("remarks"));

                Cart cart = new Cart();
                cart.setCartId(rs.getInt("cartId"));
                cart.setFoodId(rs.getInt("foodId"));
                cart.setBusinessId(rs.getInt("businessId"));
                cart.setUserId(rs.getString("userId"));
                cart.setQuantity(rs.getInt("quantity"));
                cart.setBusiness(business);
                cart.setFood(food);
                cart.setBusiness(business);
                list.add(cart);
            }
        }finally {
            DBUtil.close(rs,pst);
        }
        return list;
    }

    @Override
    public int saveCart(String userId, Integer businessId, Integer foodId) throws Exception {
        int rs = 1;
        String sql = "insert into cart values(null,?,?,?,1)";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setInt(1,foodId);
            pst.setInt(2,businessId);
            pst.setString(3,userId);
            rs = pst.executeUpdate();
        }finally {
            DBUtil.close(pst);
        }
        return rs;
    }

    @Override
    public int updateCart(String userId, Integer businessId, Integer foodId, Integer quantity) throws Exception {
        int rs = 1;
        String sql = "update cart set quantity=? where userId=? and businessId=? and foodId=?";
        try{
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setInt(1,quantity);
            pst.setInt(4,foodId);
            pst.setInt(3,businessId);
            pst.setString(2,userId);
            rs = pst.executeUpdate();
        } finally {
            DBUtil.close(pst);
        }
        return rs;
    }

    @Override
    public int removeCart(String userId, Integer businessId, Integer foodId) throws Exception {
        int rs = 1;
        String sql = "delete from cart where userId=? and businessId=? and foodId=?";
        try{
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setInt(3,foodId);
            pst.setInt(2,businessId);
            pst.setString(1,userId);
            rs = pst.executeUpdate();
        }finally {
            DBUtil.close(pst);
        }
        return rs;
    }
}
